package com.mystyle.le.weekproject.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.beans.PassList;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class PictureActivity extends AppCompatActivity {

    private ViewPager picVP;
    private TextView pictv;
    private List<ImageView> list;
    private List<PassList> list1 = new ArrayList<>();
    private ImageView[] imgs;
    private int position1;
    private PopupWindow popupWindow;
    private View inflate;
    private View view;
    private boolean isCheck = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);

        //初始化控件
        initView();
        //ViewPager的滑动事件
        initPager();


    }

    private void initPopWindos() {
        inflate = LayoutInflater.from(PictureActivity.this).inflate(R.layout.popwindow_layout,null);
        popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(inflate);//设置布局
        view = LayoutInflater.from(PictureActivity.this).inflate(R.layout.activity_picture, null);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
            }
        });



    }

    private void initPager() {
        list = new ArrayList<>();
        if (list1!=null&&list1.size()>0){
            for (int i =0;i<list1.size();i++){
                imgs = new ImageView[list1.size()];
                imgs[i] = new ImageView(PictureActivity.this);
                imgs[i].setScaleType(ImageView.ScaleType.FIT_XY);
                String s = list1.get(i).getS();
                Picasso.with(PictureActivity.this).load(s).into(imgs[i]);
                imgs[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initPopWindos();
                        popupWindow.showAtLocation(view,Gravity.BOTTOM,0,0);
                        popupWindow.update();
                    }
                });

                list.add(imgs[i]);
            }
        }
        picVP.setAdapter(new MyPagerAdapter());
        picVP.setCurrentItem(position1);


//

        picVP.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                position1 = position;
               list.get(position);
                pictv.setText((position1+1)+"/"+list1.size());
//
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
            container.removeView(list.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

    private void initView() {
        picVP = ((ViewPager) findViewById(R.id.picVP));
        pictv = ((TextView) findViewById(R.id.pictv));
        Intent intent = getIntent();
        position1 = intent.getIntExtra("position",0);
        list1 = (List<PassList>)intent.getSerializableExtra("list");
//        Log.e("BBBBBB","==list1=="+list1.size());

    }
}
